//
//  selectElementTypeView.m
//  dataGathering
//
//  Created by apple on 14-11-7.
//  Copyright (c) 2014年 shuili. All rights reserved.
//

#import "selectElementTypeView.h"
#import "selectElementSubtypeView.h"
#import "currentWorkSetManager.h"

@interface selectElementTypeView ()
@property(nonatomic, retain)LocalFeatureModel *featureModel;
@end

@implementation selectElementTypeView
NSInteger selectType = 0;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _featureModel = [[currentWorkSetManager sharedInstance] getLocalFeatureModel];
    ((UITableViewController*)([[self childViewControllers] objectAtIndex:0])).tableView.delegate = self;
    ((UITableViewController*)([[self childViewControllers] objectAtIndex:0])).tableView.dataSource = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectType = [indexPath row];
    [self performSegueWithIdentifier:@"toElementsCatogerySelect" sender:self];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[_featureModel groups] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 288, 48)];
    
    NSUInteger row = [indexPath row];
    
    LocalGroupModel *group = [[_featureModel groups] objectAtIndex:row];
    

    NSString *appDocumentPath = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [appDocumentPath stringByAppendingString:@"/LayerIcons/"];
    //NSString *iconPath = [filePath stringByAppendingString:[group icon]];
    UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:@""]];
    view.frame = CGRectMake(8, 6, 32 , 32);
    view.contentMode = UIViewContentModeScaleAspectFit;
    
    UILabel *typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(48, 7, 81, 19)];
    [typeLabel setFont:[UIFont systemFontOfSize:16.0]];
    [typeLabel setTextColor:[UIColor colorWithRed:77.0/255.0 green:77.0/255.0 blue:77.0/255.0 alpha:1]];
    [typeLabel setText:[group name]];
    
    [cell.contentView addSubview:view];
    [cell.contentView addSubview:typeLabel];
    
    return cell;
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"%@ is ",[segue identifier]);

    if([segue identifier] && [[segue identifier] compare:@"toElementsCatogerySelect"] == NSOrderedSame){
        
        NSArray *typeListArray = [(LocalGroupModel*)[[_featureModel groups] objectAtIndex:selectType] layers];
        
        ((selectElementSubtypeView*)[segue destinationViewController]).tableData = typeListArray;
        ((selectElementSubtypeView*)[segue destinationViewController]).titleName= [(LocalGroupModel*)[[_featureModel groups] objectAtIndex:selectType] name];

        NSLog(@"222");
    }
    
}


@end
